1 Image Processing and Enhancement

1.1 Blurring an Image

In [221]:
import cv2
from  matplotlib import pyplot as plt


image_path = 'content/cats-dogs.png'
image = cv2.imread(image_path)
resized_image = cv2.resize(image, (2000, 1000))
resized_image_rgb = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
plt.imshow(resized_image_rgb)
plt.title('Original Image')
plt.axis('off')
plt.show()
No description has been provided for this image

1.1.1 Gaussian Blurring

In [222]:
Gaussian = cv2.GaussianBlur(resized_image, (15, 15), 0)  
Gaussian_rgb = cv2.cvtColor(Gaussian, cv2.COLOR_BGR2RGB)  
plt.imshow(Gaussian_rgb)
plt.title('Gaussian Blurred Image')
plt.axis('off')
plt.show()
No description has been provided for this image

1.1.2 Median Blur

In [223]:
median = cv2.medianBlur(resized_image, 11)  
median_rgb = cv2.cvtColor(median, cv2.COLOR_BGR2RGB)  

plt.imshow(median_rgb)
plt.title('Median Blurred Image')
plt.axis('off')
plt.show()
No description has been provided for this image

1.1.3 Bilateral Blur

In [224]:
bilateral = cv2.bilateralFilter(resized_image, 15, 150, 150)  
bilateral_rgb = cv2.cvtColor(bilateral, cv2.COLOR_BGR2RGB)  

plt.imshow(bilateral_rgb)
plt.title('Bilateral Blurred Image')
plt.axis('off')
plt.show()
No description has been provided for this image

1.2 Grayscaling of Images

1.2.1 Method 1: Using the cvcvtColor() function

In [225]:
import cv2
tomatoes = 'content/tomatoes.png'
image = cv2.imread(tomatoes)

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow('Grayscale', gray_image)
cv2.waitKey(0)  
cv2.destroyAllWindows()
In [226]:
import matplotlib.pyplot as plt

plt.figure(figsize=(16, 6))


plt.subplot(1, 2, 1)
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.title("Original Image")
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(gray_image, cmap='gray')
plt.title("Grayscale Image")
plt.axis('off')

plt.show()
No description has been provided for this image

1.2.2 Method 2: Using the cvimread() function with flag=zero

In [227]:
import cv2

img = cv2.imread(tomatoes, 0)

cv2.imshow('Grayscale Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
In [228]:
im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(im)
Out[228]:
<matplotlib.image.AxesImage at 0x1b395366180>
No description has been provided for this image

1.2.3 Method 1 Weighted Method (Recommended)

In [229]:
import cv2

img_weighted = cv2.imread(tomatoes)
rows, cols = img_weighted.shape[:2]

for i in range(rows):
    for j in range(cols):
        gray = 0.2989 * img_weighted[i, j][2] + 0.5870 * img_weighted[i, j][1] + 0.1140 * img_weighted[i, j][0]
        img_weighted[i, j] = [gray, gray, gray]

cv2.imshow('Grayscale Image (Weighted)', img_weighted)
cv2.waitKey(0)
cv2.destroyAllWindows()
In [230]:
imo = cv2.cvtColor(img_weighted, cv2.COLOR_BGR2RGB)
plt.imshow(imo)
Out[230]:
<matplotlib.image.AxesImage at 0x1b395366b10>
No description has been provided for this image

1.2.4 Method 2: Using the pixel manipulation (Average method)

In [231]:
import cv2

img = cv2.imread(tomatoes)
rows, cols = img.shape[:2]

for i in range(rows):
    for j in range(cols):
        # Convert to float() so the sum can exceed 255
        r = float(img[i, j, 0])
        g = float(img[i, j, 1])
        b = float(img[i, j, 2])
        
        gray = int((r + g + b) / 3)
    
        img[i, j] = [gray, gray, gray]
cv2.imshow('Grayscale Image (Average)', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
In [232]:
im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(im)
Out[232]:
<matplotlib.image.AxesImage at 0x1b39a019370>
No description has been provided for this image
In [ ]:
 

1.3 Scaling, Rotating, Shifting and Edge Detection

1.3.1 Image Resizing

In [233]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

morti = 'content/Ganeshji-org.webp'

image = cv2.imread(morti)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
scale_factor_1 = 3.0  
scale_factor_2 = 1/3.0
height, width = image_rgb.shape[:2]
new_height = int(height * scale_factor_1)
new_width = int(width * scale_factor_1)

zoomed_image = cv2.resize(src =image_rgb, 
                          dsize=(new_width, new_height), 
                          interpolation=cv2.INTER_CUBIC)
                          
new_height1 = int(height * scale_factor_2)
new_width1 = int(width * scale_factor_2)
scaled_image = cv2.resize(src= image_rgb, 
                          dsize =(new_width1, new_height1), 
                          interpolation=cv2.INTER_AREA)

fig, axs = plt.subplots(1, 3, figsize=(10, 4))
axs[0].imshow(image_rgb)
axs[0].set_title('Original Image Shape:'+str(image_rgb.shape))
axs[1].imshow(zoomed_image)
axs[1].set_title('Zoomed Image Shape:'+str(zoomed_image.shape))
axs[2].imshow(scaled_image)
axs[2].set_title('Scaled Image Shape:'+str(scaled_image.shape))

for ax in axs:
    ax.set_xticks([])
    ax.set_yticks([])

plt.tight_layout()
plt.show()
No description has been provided for this image

1.3.2 Image Rotation

In [234]:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(morti)
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
center = (image_rgb.shape[1] // 2, image_rgb.shape[0] // 2)
angle = 30
scale = 1
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
rotated_image = cv2.warpAffine(image_rgb, rotation_matrix, (img.shape[1], img.shape[0]))

fig, axs = plt.subplots(1, 2, figsize=(7, 4))
axs[0].imshow(image_rgb)
axs[0].set_title('Original Image')
axs[1].imshow(rotated_image)
axs[1].set_title('Image Rotation')
for ax in axs:
    ax.set_xticks([])
    ax.set_yticks([])
    
plt.tight_layout()
plt.show()
No description has been provided for this image

1.3.3 Image Translation

In [235]:
import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread(morti)
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
width, height = image_rgb.shape[1], image_rgb.shape[0]

tx, ty = 100, 70
translation_matrix = np.array([[1, 0, tx], [0, 1, ty]], dtype=np.float32)
translated_image = cv2.warpAffine(image_rgb, translation_matrix, (width, height))

fig, axs = plt.subplots(1, 2, figsize=(7, 4))
axs[0].imshow(image_rgb), axs[0].set_title('Original Image')
axs[1].imshow(translated_image), axs[1].set_title('Image Translation')

for ax in axs:
    ax.set_xticks([]), ax.set_yticks([])

plt.tight_layout()
plt.show()
No description has been provided for this image

1.3.4 Image Shearing

In [236]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread(morti)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
width, height = image_rgb.shape[1], image_rgb.shape[0]

shearX, shearY = -0.15, 0
transformation_matrix = np.array([[1, shearX, 0], [0, 1, shearY]], dtype=np.float32)
sheared_image = cv2.warpAffine(image_rgb, transformation_matrix, (width, height))

fig, axs = plt.subplots(1, 2, figsize=(7, 4))
axs[0].imshow(image_rgb), axs[0].set_title('Original Image')
axs[1].imshow(sheared_image), axs[1].set_title('Sheared Image')

for ax in axs:
    ax.set_xticks([]), ax.set_yticks([])

plt.tight_layout()
plt.show()
No description has been provided for this image

1.3.5 Image Normalization

In [237]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread(morti)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
b, g, r = cv2.split(image_rgb)

b_normalized = cv2.normalize(b.astype('float'), None, 0, 1, cv2.NORM_MINMAX)
g_normalized = cv2.normalize(g.astype('float'), None, 0, 1, cv2.NORM_MINMAX)
r_normalized = cv2.normalize(r.astype('float'), None, 0, 1, cv2.NORM_MINMAX)

normalized_image = cv2.merge((b_normalized, g_normalized, r_normalized))
print(normalized_image[:, :, 0])

plt.imshow(normalized_image)
plt.xticks([]), 
plt.yticks([]), 
plt.title('Normalized Image')
plt.show()
[[0.08627451 0.08627451 0.08627451 ... 0.08627451 0.10588235 0.16078431]
 [0.08627451 0.08627451 0.08627451 ... 0.05882353 0.08235294 0.1372549 ]
 [0.08627451 0.08627451 0.08627451 ... 0.0745098  0.09411765 0.14901961]
 ...
 [0.08235294 0.02745098 0.00784314 ... 0.08235294 0.09019608 0.11764706]
 [0.10588235 0.05098039 0.03137255 ... 0.09019608 0.09803922 0.1254902 ]
 [0.11764706 0.0627451  0.04705882 ... 0.09803922 0.10980392 0.1372549 ]]
No description has been provided for this image

1.3.6 Edge detection of Image

In [238]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread(morti)
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
edges = cv2.Canny(image_rgb, 100, 700)

fig, axs = plt.subplots(1, 2, figsize=(7, 4))
axs[0].imshow(image_rgb), axs[0].set_title('Original Image')
axs[1].imshow(edges), axs[1].set_title('Image Edges')

for ax in axs:
    ax.set_xticks([]), ax.set_yticks([])

plt.tight_layout()
plt.show()
No description has been provided for this image

1.3.7 Image Blurring

In [239]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread(morti)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
blurred = cv2.GaussianBlur(image, (3, 3), 0)
blurred_rgb = cv2.cvtColor(blurred, cv2.COLOR_BGR2RGB)

fig, axs = plt.subplots(1, 2, figsize=(7, 4))
axs[0].imshow(image_rgb), axs[0].set_title('Original Image')
axs[1].imshow(blurred_rgb), axs[1].set_title('Blurred Image')

for ax in axs:
    ax.set_xticks([]), ax.set_yticks([])

plt.tight_layout()
plt.show()
No description has been provided for this image

1.3.8 Morphological Image Processing

In [240]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread(morti)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kernel = np.ones((3, 3), np.uint8)

dilated = cv2.dilate(image_gray, kernel, iterations=2)
eroded = cv2.erode(image_gray, kernel, iterations=2)
opening = cv2.morphologyEx(image_gray, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(image_gray, cv2.MORPH_CLOSE, kernel)

fig, axs = plt.subplots(2, 2, figsize=(7, 7))
axs[0, 0].imshow(dilated, cmap='Greys'), axs[0, 0].set_title('Dilated Image')
axs[0, 1].imshow(eroded, cmap='Greys'), axs[0, 1].set_title('Eroded Image')
axs[1, 0].imshow(opening, cmap='Greys'), axs[1, 0].set_title('Opening')
axs[1, 1].imshow(closing, cmap='Greys'), axs[1, 1].set_title('Closing')

for ax in axs.flatten():
    ax.set_xticks([]), ax.set_yticks([])

plt.tight_layout()
plt.show()
No description has been provided for this image

1.4 Intensity Transformation Operations on Images

1.4.1 Log Transformations

In [241]:
import cv2
import numpy as np

camer = 'content/sample.jpg'

# Open the image.
img = cv2.imread(camer)

# Apply log transform.
c = 255/(np.log(1 + np.max(img)))
log_transformed = c * np.log(1 + img)

# Specify the data type.
log_transformed = np.array(log_transformed, dtype = np.uint8)

# Save the output.
# cv2.imwrite('log_transformed.jpg', log_transformed)

plt.imshow(log_transformed)
Out[241]:
<matplotlib.image.AxesImage at 0x1b3818841d0>
No description has been provided for this image

1.4.2 Power-Law (Gamma) Transformation

In [242]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread(camer)

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

gammas = [0.1, 0.5, 1.2, 2.2]
plt.figure(figsize=(10, 8))

for i, gamma in enumerate(gammas):
    # Apply gamma correction
    gamma_corrected = np.array(255 * (img_rgb / 255) ** gamma, dtype='uint8')
    
    # Create a subplot in a 2x2 grid
    plt.subplot(2, 2, i + 1)
    plt.imshow(gamma_corrected)
    plt.title(f'Gamma: {gamma}')
    plt.axis('off') # Hide x/y axes for a cleaner look

plt.tight_layout()
plt.show()
No description has been provided for this image

1.4.3 Piecewise-Linear Transformation Functions

In [243]:
import cv2
import numpy as np

# Function to map each intensity level to output intensity level.
def pixelVal(pix, r1, s1, r2, s2):
    if (0 <= pix and pix <= r1):
        return (s1 / r1)*pix
    elif (r1 < pix and pix <= r2):
        return ((s2 - s1)/(r2 - r1)) * (pix - r1) + s1
    else:
        return ((255 - s2)/(255 - r2)) * (pix - r2) + s2

# Open the image.
img = cv2.imread(camer)

# Define parameters.
r1 = 70
s1 = 0
r2 = 140
s2 = 255

# Vectorize the function to apply it to each value in the Numpy array.
pixelVal_vec = np.vectorize(pixelVal)

# Apply contrast stretching.
contrast_stretched = pixelVal_vec(img, r1, s1, r2, s2)

# Save edited image.
# cv2.imwrite('contrast_stretch.jpg', contrast_stretched)

plt.imshow(contrast_stretched)
plt.title('Contrast Stretched Image')
plt.axis('off') 
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Got range [0.0..255.0].
Out[243]:
(np.float64(-0.5), np.float64(1299.5), np.float64(866.5), np.float64(-0.5))
No description has been provided for this image

1.5 Image Translation

1.5.1 Example 1: Translating the Image Right and Down

In [244]:
import cv2
import numpy as np
# from google.colab.patches import cv2_imshow

image = cv2.imread(tomatoes)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

height, width = image.shape[:2]
quarter_height, quarter_width = height / 4, width / 4

T = np.float32([[1, 0, quarter_width], [0, 1, quarter_height]])

img_translation = cv2.warpAffine(image, T, (width, height))


plt.imshow(image)
plt.title('Original Image')
plt.axis('off')
plt.show()

plt.imshow(img_translation)
plt.title('Translated Image')
plt.axis('off') 
plt.show()
No description has been provided for this image
No description has been provided for this image
In [245]:
print("hello world")
hello world

1.5.2 Example 2: Performing Multiple Translations

In [246]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread(tomatoes)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

rows, cols, _ = img.shape

M_left = np.float32([[1, 0, -50], [0, 1, 0]])
M_right = np.float32([[1, 0, 50], [0, 1, 0]])
M_top = np.float32([[1, 0, 0], [0, 1, 50]])
M_bottom = np.float32([[1, 0, 0], [0, 1, -50]])

img_left = cv2.warpAffine(img, M_left, (cols, rows))
img_right = cv2.warpAffine(img, M_right, (cols, rows))
img_top = cv2.warpAffine(img, M_top, (cols, rows))
img_bottom = cv2.warpAffine(img, M_bottom, (cols, rows))

plt.subplot(221), plt.imshow(img_left), plt.title('Left')
plt.subplot(222), plt.imshow(img_right), plt.title('Right')
plt.subplot(223), plt.imshow(img_top), plt.title('Top')
plt.subplot(224), plt.imshow(img_bottom), plt.title('Bottom')
plt.show()
No description has been provided for this image

1.6 Image Pyramid

1.6.1 Pyramid Down with cvpyrDown()

In [247]:
import cv2
import numpy as np
# from google.colab.patches import cv2_imshow

image = cv2.imread(tomatoes)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

downsampled_image = cv2.pyrDown(image)

fig, axes = plt.subplots(1, 2, figsize=(12, 6))
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')

axes[1].imshow(downsampled_image)
axes[1].set_title('Downsampled Image')
axes[1].axis('off')

plt.tight_layout()
plt.show()
No description has been provided for this image

1.6.2 Pyramid Up with cvpyrUp()

In [248]:
import cv2
import numpy as np


image = cv2.imread(tomatoes)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

upsampled_image = cv2.pyrUp(image)

plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(upsampled_image)
plt.title('Upsampled Image')
plt.axis('off')

plt.tight_layout()
plt.show()
No description has been provided for this image

1.6.3 Building a Gaussian Pyramid (Multiple Levels)

In [249]:
import cv2

image = cv2.imread(camer)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

pyramid = [image]

for i in range(3): 
    image = cv2.pyrDown(image)
    pyramid.append(image)

for i in range(len(pyramid)-1, -1, -1):  
    print(f"Camera Level {i}")
    plt.imshow(pyramid[i])
    plt.title(f'Pyramid Level {i}')
    plt.axis('off')
    plt.show()
    
cv2.destroyAllWindows()
Camera Level 3
No description has been provided for this image
Camera Level 2
No description has been provided for this image
Camera Level 1
No description has been provided for this image
Camera Level 0
No description has been provided for this image

1.7 Histograms Equalization

1.7.1 Step Importing Libraries

In [250]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

1.7.2 Step Applying Histogram Equalization

In [251]:
img = cv2.imread(tomatoes, 0)  
equ = cv2.equalizeHist(img)
res = np.hstack((img, equ))

1.7.3 Step Displaying the Result

In [252]:
plt.figure(figsize=(12, 6))
res = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
plt.imshow(res)  
plt.title("Original vs Equalized Image")
plt.axis('off')  
plt.show()
No description has been provided for this image

1.8 Convert an image from one color space to another

1.8.1 Example 1: Convert BGR to Grayscale

In [253]:
import cv2
src = cv2.imread(tomatoes)  # Read the image
src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)  # Convert to RGB for displaying with Matplotlib

# Convert to Grayscale
gray_image = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
gray_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2RGB) 


plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(src)
plt.title("Original Image")
plt.axis('off') 

plt.subplot(1, 2, 2)
plt.imshow(gray_image)
plt.title("Grayscale Image")
plt.axis('off')
Out[253]:
(np.float64(-0.5), np.float64(296.5), np.float64(199.5), np.float64(-0.5))
No description has been provided for this image

1.8.2 Example 2: Convert BGR to HSV

In [254]:
import cv2
src = cv2.imread(tomatoes)  # Read the image

# Convert to HSV
hsv_image = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)

cv2.imshow("HSV Image", hsv_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
In [255]:
imag = cv2.imread('content/hsv-tomato.png')
imag = cv2.cvtColor(imag, cv2.COLOR_BGR2RGB)
In [256]:
plt.imshow(imag)
Out[256]:
<matplotlib.image.AxesImage at 0x1b3801e8290>
No description has been provided for this image

1.8.3 Example 3: Convert BGR to RGB (For Matplotlib)

In [257]:
import cv2
import matplotlib.pyplot as plt
src = cv2.imread(tomatoes)

# Convert from BGR to RGB
rgb_image = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)

# Display with Matplotlib
plt.imshow(rgb_image)
plt.title("RGB Image for Matplotlib")
plt.axis('off')
plt.show()
No description has been provided for this image

1.9 Visualizing image in different color spaces

1.9.1 RGB Image

In [258]:
# Python program to read image as RGB

# Importing cv2 and matplotlib module
import cv2
import matplotlib.pyplot as plt

# reads image as RGB
img = cv2.imread(tomatoes)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# shows the image
plt.imshow(img)
Out[258]:
<matplotlib.image.AxesImage at 0x1b3856add30>
No description has been provided for this image

1.9.2 Gray Scale Image

In [259]:
def rgb_for_metplot(img, mod, title):
    image = cv2.cvtColor(img, mod)
    plt.figure()
    plt.imshow(image)
    plt.title(title)
    plt.axis('off')
    plt.show() 
In [ ]:
 
In [260]:
# Python program to read image as GrayScale

# Importing cv2 module
import cv2

# Reads image as gray scale
img = cv2.imread(tomatoes) 

# We can alternatively convert
# image by using cv2color
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)

# Shows the image
# cv2.imshow('image', img) 
# cv2.waitKey(0)         
# cv2.destroyAllWindows()

rgb_for_metplot(img,cv2.COLOR_GRAY2RGB , "Grayscale Image")
No description has been provided for this image
In [261]:
rgb_for_metplot(img,cv2.COLOR_GRAY2RGB, "Random stuf")
No description has been provided for this image

1.9.3 YCrCb Color Space

In [262]:
# Python program to read image
# as YCrCb color space

# Import cv2 module
import cv2

# Reads the image
img = cv2.imread(tomatoes)

# Convert to YCrCb color space
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)

# Shows the image
cv2.imshow('image', img) 

cv2.waitKey(0)         
cv2.destroyAllWindows()

# rgb_for_metplot(img, cv2.COLOR_YCrCb2RGB)
plt.imshow(img)
Out[262]:
<matplotlib.image.AxesImage at 0x1b3818ab260>
No description has been provided for this image

1.9.4 HSV color space

In [263]:
# Python program to read image
# as HSV color space

# Importing cv2 module
import cv2

# Reads the image
img = cv2.imread(tomatoes)

# Converts to HSV color space
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Shows the image
cv2.imshow('image', img) 

cv2.waitKey(0)         
cv2.destroyAllWindows()

1.9.5 LAB color space

In [264]:
# Python program to read image
# as LAB color space

# Importing cv2 module
import cv2

# Reads the image
img = cv2.imread(tomatoes)

# Converts to LAB color space
img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)

# Shows the image
cv2.imshow('image', img) 

cv2.waitKey(0)         
cv2.destroyAllWindows()

1.9.6 Edge map of image

In [265]:
# Python program to read image
# as EdgeMap

# Importing cv2 module
import cv2

# Reads the image
img = cv2.imread(tomatoes)


laplacian = cv2.Laplacian(img, cv2.CV_64F)
cv2.imshow('EdgeMap', laplacian) 

cv2.waitKey(0)         
cv2.destroyAllWindows()

1.9.7 Heat map of image

In [266]:
import cv2
import matplotlib.pyplot as plt

# 1. Image load karein
img = cv2.imread(tomatoes)

# 2. Image ko Grayscale mein convert karein (Zaroori Step)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 3. Ab 'hot' colormap apply karein
plt.figure(figsize=(8, 6))
plt.imshow(gray_img, cmap='hot') 

# Colorbar dikhane ke liye takay intensity samajh aaye
plt.colorbar() 
plt.title('Heatmap (Hot Colormap)')
plt.axis('off')
plt.show()
No description has been provided for this image

1.9.8 Spectral Image map

In [267]:
import cv2
import matplotlib.pyplot as plt

# 1. Image load karein
img = cv2.imread(tomatoes)

# 2. Image ko Grayscale (Single Channel) mein convert karein
# Spectral map tabhi apply hoga jab image 2D array hogi
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 3. Ab 'nipy_spectral' colormap apply karein
plt.figure(figsize=(10, 7))
plt.imshow(gray_img, cmap='nipy_spectral')

# Colorbar lazmi lagayein takay pata chale kaunsi intensity kis color ki hai
plt.colorbar()
plt.title('Spectral Map Visualization')
plt.axis('off')
plt.show()
No description has been provided for this image

1.10 Create Border around Images

In [268]:
import cv2

image = cv2.imread(tomatoes)
image = cv2.copyMakeBorder( image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=(0, 0, 0))

cv2.imshow("Bordered Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

rgb_for_metplot(image, cv2.COLOR_BGR2RGB, "Random stuf")
No description has been provided for this image

1.10.1 Examples 1

In [269]:
import cv2

image = cv2.imread(tomatoes)

border_reflect = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REFLECT)
border_reflect_101 = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REFLECT_101)
border_replicate = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REPLICATE)



cv2.imshow("Border Reflect", border_reflect)
cv2.imshow("Border Reflect 101", border_reflect_101)
cv2.imshow("Border Replicate", border_replicate)

cv2.waitKey(0)
cv2.destroyAllWindows()


rgb_for_metplot(border_reflect, cv2.COLOR_BGR2RGB, "Border Reflect")
rgb_for_metplot(border_reflect_101, cv2.COLOR_BGR2RGB, "Border Reflect 101")
rgb_for_metplot(border_replicate, cv2.COLOR_BGR2RGB, "Border Replicate")
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

1.10.2 Examples 2

In [270]:
import cv2

image = cv2.imread(tomatoes)
bordered_image = cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=(0, 0, 255))

cv2.imshow("Red Border Image", bordered_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

rgb_for_metplot(bordered_image, cv2.COLOR_BGR2RGB, "Red Border Image")
No description has been provided for this image

2 Image Segmentation and Thresholding

2.1 Simple Thresholding

2.1.1 Step 1: Import libraries and Image Preparation

In [271]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread(tomatoes)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

2.1.2 Step 2: Helper Function

In [272]:
def show_image(img, title):
    plt.imshow(img, cmap='gray')
    plt.title(title)
    plt.axis('off')
    plt.show()

2.1.3 Step 3: Display the Original Image

In [273]:
show_image(gray_image, 'Original Grayscale Image')
No description has been provided for this image

2.1.4 Step 4: Binary Threshold

In [274]:
_, thresh_binary = cv2.threshold(gray_image, 120, 255, cv2.THRESH_BINARY)
show_image(thresh_binary, 'Binary Threshold ')
No description has been provided for this image

2.1.5 Step 5: Binary Threshold Inverted

In [275]:
_, thresh_binary_inv = cv2.threshold(
    gray_image, 120, 255, cv2.THRESH_BINARY_INV)
show_image(thresh_binary_inv, 'Binary Threshold Inverted ')
No description has been provided for this image

2.1.6 Step 6: Truncated Threshold

In [276]:
_, thresh_trunc = cv2.threshold(gray_image, 120, 255, cv2.THRESH_TRUNC)
show_image(thresh_trunc, 'Truncated Threshold')
No description has been provided for this image

2.1.7 Step 7: To Zero Threshold

In [277]:
_, thresh_tozero = cv2.threshold(gray_image, 120, 255, cv2.THRESH_TOZERO)
show_image(thresh_tozero, 'Set to 0 ')
No description has been provided for this image

2.1.8 Step 8: To Zero Inverted Threshold

In [278]:
_, thresh_tozero_inv = cv2.threshold(
    gray_image, 120, 255, cv2.THRESH_TOZERO_INV)
show_image(thresh_tozero_inv, 'Set to 0 Inverted')
No description has been provided for this image

2.2 Adaptive Thresholding

2.2.1 Step 1: Import libraries and Image Preparation

In [279]:
import cv2
import matplotlib.pyplot as plt

image = cv2.imread(morti)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

2.2.2 Step 2: Helper Function

In [280]:
def show_image(img, title):
    plt.imshow(img, cmap='gray')
    plt.title(title)
    plt.axis('off')
    plt.show()

2.2.3 Step 3: Display Original Image

In [281]:
show_image(gray_image, "Original Grayscale Image")
No description has been provided for this image

2.2.4 Step 4: Adaptive Mean Thresholding

In [282]:
thresh_mean = cv2.adaptiveThreshold(
    gray_image, 255,
    cv2.ADAPTIVE_THRESH_MEAN_C,
    cv2.THRESH_BINARY,
    199, 5
)
show_image(thresh_mean, "Adaptive Mean Thresholding")
No description has been provided for this image

2.2.5 Step 5: Adaptive Gaussian Threosholding

In [283]:
thresh_gauss = cv2.adaptiveThreshold(
    gray_image, 255,
    cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
    cv2.THRESH_BINARY,
    199, 5
)
show_image(thresh_gauss, "Adaptive Gaussian Thresholding")
No description has been provided for this image

3 Morphological Operations & Filtering

3.1 Erosion and Dilation of images

3.1.1 Step 2: Load Input Image and Define the Structuring Elements(Kernel)

In [284]:
img = cv2.imread(tomatoes, 0)
plt.imshow(img, cmap='gray')
plt.title("Original Image")
plt.axis('off')
plt.show()
kernel = np.ones((5, 5), np.uint8)
No description has been provided for this image

3.1.2 Step 3: Apply Erosion

In [285]:
img_erosion = cv2.erode(img, kernel, iterations=1)

plt.imshow(img_erosion, cmap='gray')
plt.title("After Erosion")
plt.axis('off')
plt.show()
No description has been provided for this image

3.1.3 Step 4: Apply Dilation

In [286]:
img_dilation = cv2.dilate(img, kernel, iterations=1)

plt.imshow(img_dilation, cmap='gray')
plt.title("After Dilation")
plt.axis('off')
plt.show()
No description has been provided for this image

3.2 Bilateral Filtering

3.2.1 Bilateral Filter: an Additional Edge Term

In [287]:
import cv2

# Read the image.
img = cv2.imread(tomatoes)

# Apply bilateral filter with d = 15, 
# sigmaColor = sigmaSpace = 75.
bilateral = cv2.bilateralFilter(img, 15, 75, 75)

# Save the output.
# cv2.imwrite('taj_bilateral.jpg', bilateral)

rgb_for_metplot(bilateral, cv2.COLOR_BGR2RGB, "Bilateral Filtered Image")
No description has been provided for this image

3.3 Denoising of colored images

3.3.1 Example: Denoising a Noisy Image

In [288]:
# Importing required libraries
import numpy as np
import cv2
from matplotlib import pyplot as plt

# Reading the noisy image from file
img = cv2.imread(tomatoes)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Applying denoising filter
dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 15)

# Displaying original and denoised images
plt.subplot(121), plt.imshow(img), plt.title('Original Image')
plt.subplot(122), plt.imshow(dst), plt.title('Denoised Image')
plt.show()
No description has been provided for this image

3.4 Filter Color with OpenCV

3.4.1 Code Example: Real-time Blue Color Detection

In [289]:
import cv2
import numpy as np

cap = cv2.VideoCapture(0)  # Start webcam

while True:
    _, frame = cap.read()
    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    # Define range for blue color in HSV
    lower_blue = np.array([60, 35, 140])
    upper_blue = np.array([180, 255, 255])

    # Create mask
    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    
    # Filter the blue region
    result = cv2.bitwise_and(frame, frame, mask=mask)

    # Show frames
    cv2.imshow('Original Frame', frame)
    cv2.imshow('Blue Mask', mask)
    cv2.imshow('Blue Filtered Result', result)

    # Press 'q' to quit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break  # Exit the loop when 'q' is pressed

cap.release()
cv2.destroyAllWindows()
print("Exiting...")  # Confirm exit
Exiting...

4 Advanced Image Manipulation & Background Subtraction

4.1 Image Inpainting using OpenCV

4.1.1 Creating the mask manually in OpenCV

In [290]:
import cv2
import numpy as np

# reading the damaged image
damaged_img = cv2.imread(filename='content/cat.png')

# get the shape of the image
height, width = damaged_img.shape[0], damaged_img.shape[1]

# Converting all pixels greater than zero to black while black becomes white
for i in range(height):
    for j in range(width):
        if damaged_img[i, j].sum() > 0:
            damaged_img[i, j] = 0
        else:
            damaged_img[i, j] = [255, 255, 255]

# saving the mask 
mask = damaged_img
cv2.imwrite('content/cat_damaged.png', mask)

# displaying mask
cv2.imshow("damaged image mask", mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
In [291]:
import numpy as np
import cv2

# Open the image.
img = cv2.imread('content/cat_damaged.png')

# Load the mask.
mask = cv2.imread('content/mask.png', 0)

# Inpaint.
dst = cv2.inpaint(img, mask, 3, cv2.INPAINT_NS)

# Write the output.
cv2.imwrite('content/cat_inpainted.png', dst)
Out[291]:
True

4.2 Image Registration

4.2.1 Code Section

In [292]:
import cv2
import numpy as np

# Open the image files.
img1_color = cv2.imread("content/im2-copy.webp")  # Image to be aligned.
img2_color = cv2.imread("content/im1-copy.webp")    # Reference image.

# Convert to grayscale.
img1 = cv2.cvtColor(img1_color, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img2_color, cv2.COLOR_BGR2GRAY)
height, width = img2.shape

# Create ORB detector with 5000 features.
orb_detector = cv2.ORB_create(5000)

# Find keypoints and descriptors.
# The first arg is the image, second arg is the mask
#  (which is not required in this case).
kp1, d1 = orb_detector.detectAndCompute(img1, None)
kp2, d2 = orb_detector.detectAndCompute(img2, None)

# Match features between the two images.
# We create a Brute Force matcher with 
# Hamming distance as measurement mode.
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

# Match the two sets of descriptors.
matches = matcher.match(d1, d2)

# Sort matches on the basis of their Hamming distance.
# 'matches' ko list mein convert kar ke sort karein
matches = sorted(matches, key = lambda x: x.distance)

# Take the top 90 % matches forward.
matches = matches[:int(len(matches)*0.9)]
no_of_matches = len(matches)

# Define empty matrices of shape no_of_matches * 2.
p1 = np.zeros((no_of_matches, 2))
p2 = np.zeros((no_of_matches, 2))

for i in range(len(matches)):
  p1[i, :] = kp1[matches[i].queryIdx].pt
  p2[i, :] = kp2[matches[i].trainIdx].pt

# Find the homography matrix.
homography, mask = cv2.findHomography(p1, p2, cv2.RANSAC)

# Use this matrix to transform the
# colored image wrt the reference image.
transformed_img = cv2.warpPerspective(img1_color,
                    homography, (width, height))

# Save the output.
cv2.imwrite('content/output.jpg', transformed_img)

plt.imshow(cv2.cvtColor(transformed_img, cv2.COLOR_BGR2RGB))
plt.title("Aligned Image")
plt.axis('off')
plt.show()
No description has been provided for this image

4.3 Background subtraction

4.3.1 Example

In [293]:
import numpy as np
import cv2

# Load video file
cap = cv2.VideoCapture('content/people_walk.mp4')

# Create background subtractor (MOG2 handles shadows well)
fgbg = cv2.createBackgroundSubtractorMOG2()

while True:
    ret, frame = cap.read()
    if not ret:
        break  # Stop if video ends

    # Apply background subtraction
    fgmask = fgbg.apply(frame)

    # Show original and foreground mask side by side
    cv2.imshow('Original Frame', frame)
    cv2.imshow('Foreground Mask', fgmask)

    # Press 'Esc' to exit
    if cv2.waitKey(30) & 0xFF == 27:
        break

# Release resources
cap.release()
cv2.destroyAllWindows()

4.4 Background Subtraction in an Image using Concept of Running Average

In [294]:
import cv2
import numpy as np

# Capture video from webcam
cap = cv2.VideoCapture(0)

# Read the first frame and convert to float
_, img = cap.read()
averageValue1 = np.float32(img)

while True:
    # Capture next frame
    _, img = cap.read()
    
    # Update background model
    cv2.accumulateWeighted(img, averageValue1, 0.02)
    
    # Convert back to 8-bit for display
    resultingFrames1 = cv2.convertScaleAbs(averageValue1)

    # Show both original and background model
    cv2.imshow('Original Frame', img)
    cv2.imshow('Background (Running Average)', resultingFrames1)
    
    # Exit on Esc key
    if cv2.waitKey(30) & 0xFF == 27:
        break

# Cleanup
cap.release()
cv2.destroyAllWindows()

4.5 Foreground Extraction in an Image using Grabcut Algorithm

4.5.1 Python Implementation: Foreground Segmentation and Extraction

In [295]:
# Python program to illustrate foreground extraction using GrabCut algorithm

# organize imports
import numpy as np
import cv2
from matplotlib import pyplot as plt
 
# path to input image specified and 
# image is loaded with imread command
image = cv2.imread("content/another.png")
 

mask = np.zeros(image.shape[:2], np.uint8)
 

backgroundModel = np.zeros((1, 65), np.float64)
foregroundModel = np.zeros((1, 65), np.float64)
 

rectangle = (20, 100, 150, 150)
 

cv2.grabCut(image, mask, rectangle,  
            backgroundModel, foregroundModel,
            3, cv2.GC_INIT_WITH_RECT)
 

mask2 = np.where((mask == 2)|(mask == 0), 0, 1).astype('uint8')
 

image_segmented = image * mask2[:, :, np.newaxis]
 
# output segmented image with colorbar
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')

# Display the segmented image
plt.subplot(1, 2, 2)
plt.title('Segmented Image')
plt.imshow(cv2.cvtColor(image_segmented, cv2.COLOR_BGR2RGB))
plt.axis('off')

plt.show()
No description has been provided for this image

5 Feature Detection and Description

5.1 Line detection using Houghline method

5.1.1 Basics of Houghline Method

In [296]:
# Python program to illustrate HoughLine
# method for line detection
import cv2
import numpy as np

# Reading the required image in
# which operations are to be done.
# Make sure that the image is in the same
# directory in which this python program is
img = cv2.imread("content/onw-image.png")

# Convert the img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply edge detection method on the image
edges = cv2.Canny(gray, 50, 150, apertureSize=3)

# This returns an array of r and theta values
lines = cv2.HoughLines(edges, 1, np.pi/180, 200)

# The below for loop runs till r and theta values
# are in the range of the 2d array
for r_theta in lines:
    arr = np.array(r_theta[0], dtype=np.float64)
    r, theta = arr
    # Stores the value of cos(theta) in a
    a = np.cos(theta)

    # Stores the value of sin(theta) in b
    b = np.sin(theta)

    # x0 stores the value rcos(theta)
    x0 = a*r

    # y0 stores the value rsin(theta)
    y0 = b*r

    # x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
    x1 = int(x0 + 1000*(-b))

    # y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
    y1 = int(y0 + 1000*(a))

    # x2 stores the rounded off value of (rcos(theta)+1000sin(theta))
    x2 = int(x0 - 1000*(-b))

    # y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
    y2 = int(y0 - 1000*(a))

    # cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
    # (0,0,255) denotes the colour of the line to be
    # drawn. In this case, it is red.
    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)

# All the changes made in the input image are finally
# written on a new image houghlines.jpg
cv2.imwrite('linesDetected.jpg', img)

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("Hough Lines Detected")
plt.axis('off')
plt.show()
No description has been provided for this image
In [297]:
print(img)
[[[  0   0 255]
  [  0   0 255]
  [  0   0 255]
  ...
  [ 82  78  78]
  [ 86  83  82]
  [ 83  79  78]]

 [[  0   0 255]
  [  0   0 255]
  [  0   0 255]
  ...
  [100  98  97]
  [101  99  98]
  [ 96  94  93]]

 [[  0   0 255]
  [  0   0 255]
  [  0   0 255]
  ...
  [123 124 122]
  [118 120 119]
  [119 121 119]]

 ...

 [[  0   0 255]
  [  0   0 255]
  [  0   0 255]
  ...
  [180 184 179]
  [180 184 179]
  [180 184 179]]

 [[  0   0 255]
  [  0   0 255]
  [  0   0 255]
  ...
  [180 184 179]
  [180 184 179]
  [180 184 179]]

 [[  0   0 255]
  [  0   0 255]
  [  0   0 255]
  ...
  [180 184 179]
  [180 184 179]
  [180 184 179]]]

5.1.2 Alternate simpler method for directly extracting points:

In [298]:
import cv2
import numpy as np

# Read image
image = cv2.imread('content/onw-image.png')

# Convert image to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

# Use canny edge detection
edges = cv2.Canny(gray,50,150,apertureSize=3)

# Apply HoughLinesP method to 
# to directly obtain line end points
lines_list =[]
lines = cv2.HoughLinesP(
            edges, # Input edge image
            1, # Distance resolution in pixels
            np.pi/180, # Angle resolution in radians
            threshold=100, # Min number of votes for valid line
            minLineLength=5, # Min allowed length of line
            maxLineGap=10 # Max allowed gap between line for joining them
            )

# Iterate over points
for points in lines:
      # Extracted points nested in the list
    x1,y1,x2,y2=points[0]
    # Draw the lines joing the points
    # On the original image
    cv2.line(image,(x1,y1),(x2,y2),(0,255,0),2)
    # Maintain a simples lookup list for points
    lines_list.append([(x1,y1),(x2,y2)])
    
# Save the result image
cv2.imwrite('detectedLines.png',image)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Hough Lines Detected with HoughLinesP")
plt.axis('off')
plt.show()
No description has been provided for this image

5.2 Circle Detection

5.2.1 Example: Detecting Circles in an Eye Image

In [299]:
import cv2
import numpy as np

# Read image
img = cv2.imread(camer)
output = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Reduce noise
gray = cv2.medianBlur(gray, 5)

# Detect circles
circles = cv2.HoughCircles(
    gray,
    cv2.HOUGH_GRADIENT,
    dp=1,
    minDist=100,      
    param1=100,         
    param2=40,       
    minRadius=30,       
    maxRadius=60
)

# Draw only the first detected circle
if circles is not None:
    circles = np.uint16(np.around(circles))
    x, y, r = circles[0][0]
    cv2.circle(output, (x, y), r, (0, 255, 0), 2)  # Circle outline
    cv2.circle(output, (x, y), 2, (0, 0, 255), 3)  # Center point

# Show result
cv2.imshow('Detected Circle', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

plt.imshow(cv2.cvtColor(output, cv2.COLOR_BGR2RGB))
plt.title("Hough Circle Detected")
plt.axis('off')
plt.show()
No description has been provided for this image

5.3 Detect corner of an image

5.3.1 Step 1: Import Libraries

In [300]:
import numpy as np
import cv2
from matplotlib import pyplot as plt

5.3.2 Step 2: Load and Preprocess the Image

In [301]:
img = cv2.imread(tomatoes)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
In [302]:
print(img)
[[[174 195 192]
  [174 194 191]
  [175 195 191]
  ...
  [139 179 158]
  [139 178 161]
  [142 166 153]]

 [[135 213 203]
  [136 211 201]
  [139 212 203]
  ...
  [  8 105  56]
  [ 13  65  35]
  [ 19  75  32]]

 [[137 215 207]
  [137 212 202]
  [138 211 203]
  ...
  [ 17  38  18]
  [ 26  85  36]
  [ 30 125  23]]

 ...

 [[ 31 121  46]
  [ 26 128  56]
  [ 26 133  65]
  ...
  [ 74 171 167]
  [ 77 173 161]
  [ 82 178 159]]

 [[ 32 121  44]
  [ 36 130  61]
  [ 36 133  68]
  ...
  [ 59 158 161]
  [ 59 158 157]
  [ 60 162 153]]

 [[ 33 121  43]
  [ 44 130  63]
  [ 46 132  72]
  ...
  [ 61 150 159]
  [ 52 146 151]
  [ 31 142 133]]]

5.3.3 Step 3: Detect Corners

In [303]:
corners = cv2.goodFeaturesToTrack(
    gray,
    maxCorners=27,
    qualityLevel=0.01,
    minDistance=10,
    blockSize=3,
    useHarrisDetector=False,
    k=0.04
)

5.3.4 Step 4: Draw and Display Results

In [304]:
corners = np.intp(corners)  # Convert to integer coords
for corner in corners:
    x, y = corner.ravel()
    cv2.circle(img, (x, y), radius=3, color=(0, 255, 0), thickness=-1)

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('Corners Detected')
plt.axis('off')
plt.show()
No description has been provided for this image

5.4 Corner Detection with Shi-Tomasi method

5.4.1 Implementing Shi-Tomasi Corner Detection

In [305]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread(tomatoes)

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

corners = cv2.goodFeaturesToTrack(gray_img, 100, 0.01, 10)
corners = np.int32(corners)

for i in corners:
    x, y = i.ravel()
    cv2.circle(img, (x, y), 3, (0, 0, 255), -1)  

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.imshow(img_rgb)
plt.title('Shi-Tomasi Corner Detection')
plt.axis('off')  
plt.show()
No description has been provided for this image

5.5 Corner detection with Harris Corner Detection

5.5.1 Implementing Harris Corner Detection

In [306]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

imagepath = camer
image = cv2.imread(imagepath)

operatedImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
operatedImage = np.float32(operatedImage)


dest = cv2.cornerHarris(operatedImage, 17, 21, 0.01)
dest = cv2.dilate(dest, None)

image[dest > 0.01 * dest.max()] = [0, 0, 255]
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

plt.imshow(image_rgb)
plt.title('Harris Corner Detection')
plt.axis('off')  
plt.show()
No description has been provided for this image

5.6 Find Circles and Ellipses in an Image

In [307]:
import cv2
import numpy as np

# Load image
image = cv2.imread("content/cpy.png", 0)

# Set our filtering parameters
# Initialize parameter setting using cv2.SimpleBlobDetector
params = cv2.SimpleBlobDetector_Params()

# Set Area filtering parameters
params.filterByArea = True
params.minArea = 100

# Set Circularity filtering parameters
params.filterByCircularity = True 
params.minCircularity = 0.9

# Set Convexity filtering parameters
params.filterByConvexity = True
params.minConvexity = 0.2
    
# Set inertia filtering parameters
params.filterByInertia = True
params.minInertiaRatio = 0.01

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)
    
# Detect blobs
keypoints = detector.detect(image)

# Draw blobs on our image as red circles
blank = np.zeros((1, 1)) 
blobs = cv2.drawKeypoints(image, keypoints, blank, (0, 0, 255),
                          cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

number_of_blobs = len(keypoints)
text = "Number of Circular Blobs: " + str(len(keypoints))
cv2.putText(blobs, text, (20, 550),
            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 100, 255), 2)

# Show blobs
cv2.imshow("Filtering Circular Blobs Only", blobs)
cv2.waitKey(0)
cv2.destroyAllWindows()


plt.imshow(blobs, cmap='gray')
Out[307]:
<matplotlib.image.AxesImage at 0x1b38564c9e0>
No description has been provided for this image

5.7 Document field detection

In [308]:
import numpy as np
import cv2
import matplotlib.pyplot as plt

# 1. Thresholds ko scores se thora sa kam rakha hai taake detection pakki ho
field_threshold = { 
    "prev_policy_no" : 0.40, # Score 0.43 tha, isliye 0.40 rakha
    "address"        : 0.70  # Score 0.80 tha, isliye 0.75 rakha
}

def getBoxed(img, img_gray, template, field_name):
    if template is None:
        print(f"Warning: {field_name} ka template nahi mila!")
        return img

    w, h = template.shape[::-1] 
    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    
    # Best score nikalne ke liye
    _, max_val, _, _ = cv2.minMaxLoc(res)
    # print(f"Field: {field_name} | Best Match Score: {max_val:.4f} (Threshold: {field_threshold[field_name]})")
        
    # Sirf un points ko pakrein jo threshold se upar hain
    loc = np.where(res >= field_threshold[field_name])
    
    rects = []
    for pt in zip(*loc[::-1]):
        # [x, y, w, h]
        rects.append([int(pt[0]), int(pt[1]), int(w), int(h)])
        # groupRectangles ko list do baar chahiye hoti hai agar matches kam hon
        rects.append([int(pt[0]), int(pt[1]), int(w), int(h)])
    
    if len(rects) > 0:
        # groupThreshold=1 matlab overlapping boxes ko merge karo
        # eps=0.5 thora zyada rakha hai taake door door wale boxes merge na hon
        rects, weights = cv2.groupRectangles(rects, groupThreshold=1, eps=0.5)
        
        for (x, y, w, h) in rects:
            # Yellow Box
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)
            
            # Label background (patti) taake text saaf nazar aaye
            label = f"{field_name}: {max_val:.2f}"
            cv2.rectangle(img, (x, y - 25), (x + w, y), (0, 255, 255), -1)
            cv2.putText(img, label, (x + 5, y - 7), 
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
    else:
        print(f"--- No match found for {field_name} ---")

    return img

# --- MAIN EXECUTION ---
img = cv2.imread('content/doc.png')
if img is None:
    print("Error: content/doc.png nahi mili!")
else:
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Grayscale mein hi load karein templates ko
    template_add = cv2.imread('content/doc_address.png', 0)
    template_prev = cv2.imread('content/doc_prev_policy.png', 0)

    output_img = img.copy()
    output_img = getBoxed(output_img, img_gray, template_add, 'address')
    output_img = getBoxed(output_img, img_gray, template_prev, 'prev_policy_no')

    plt.figure(figsize=(15, 10))
    plt.imshow(cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB))
    plt.title("Detected Fields Results")
    plt.axis('off')
    plt.show()
No description has been provided for this image
In [309]:
print(img)
[[[248 248 248]
  [248 248 248]
  [249 249 249]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[248 248 248]
  [248 248 248]
  [249 249 249]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[248 248 248]
  [248 248 248]
  [249 249 249]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 ...

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]]

5.8 Smile detection

5.8.1 Step 1: Importing OpenCV library

In [310]:
import cv2

5.8.2 Step 2: Loading Pre-trained Classifiers

In [311]:
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_smile.xml')

5.8.3 Step 3: Starting Webcam Capture

In [312]:
cap = cv2.VideoCapture(0)

5.8.4 Step 4: Processing Each Frame

In [313]:
while True:
    ret, frame = cap.read()
    
    if not ret:
        print("Failed to grab frame.")
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]

        smiles = smile_cascade.detectMultiScale(roi_gray, scaleFactor=1.8, minNeighbors=20, minSize=(25, 25))

        for (sx, sy, sw, sh) in smiles:
            cv2.rectangle(frame, (x + sx, y + sy), (x + sx + sw, y + sy + sh), (0, 255, 0), 2)

    cv2.imshow('Smile Detection', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    
cap.release()
cv2.destroyAllWindows()

5.9 Feature extraction and image classification using OpenCV

5.9.1 Step 1: Install OpenCV

In [ ]:
# !pip install opencv-python
Requirement already satisfied: opencv-python in c:\users\hp\onedrive\desktop\open cv\whyi\lib\site-packages (4.13.0.92)
Requirement already satisfied: numpy>=2 in c:\users\hp\onedrive\desktop\open cv\whyi\lib\site-packages (from opencv-python) (2.4.2)
[notice] A new release of pip is available: 24.0 -> 26.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip

5.9.2 Step 2: Import Libraries

In [315]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

5.9.3 Step 3: Load an Image

In [316]:
image = cv2.imread(morti)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

5.9.4 Step 4: Use Edge Detection

In [317]:
edges = cv2.Canny(gray_image, 100, 200)
plt.imshow(edges, cmap='gray')
plt.title('Edge Image')
plt.show()
No description has been provided for this image

5.9.5 Step 5: Use Feature Detectors

In [318]:
sift = cv2.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(gray_image, None)
image_with_sift = cv2.drawKeypoints(image, keypoints, None)
plt.imshow(cv2.cvtColor(image_with_sift, cv2.COLOR_BGR2RGB))
plt.title('SIFT Features')
plt.show()
No description has been provided for this image
In [319]:
orb = cv2.ORB_create()
keypoints, descriptors = orb.detectAndCompute(gray_image, None)
image_with_orb = cv2.drawKeypoints(image, keypoints, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
plt.imshow(cv2.cvtColor(image_with_orb, cv2.COLOR_BGR2RGB))
plt.title('ORB Features')
plt.show()
No description has been provided for this image

5.9.6 Step 1: Install the necessary libraries

In [ ]:
# !pip install PyWaveletspip opencv-python
ERROR: Could not find a version that satisfies the requirement PyWaveletspip (from versions: none)
ERROR: No matching distribution found for PyWaveletspip

[notice] A new release of pip is available: 24.0 -> 26.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip

5.9.7 Step 2: Importing Necessary Libraries

In [321]:
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

5.9.8 Step 2: Loading and Displaying the Image

In [322]:
img = cv2.imread("content/face.jpg")
rgb_for_metplot(img, cv2.COLOR_BGR2RGB, "Original Image")
No description has been provided for this image

5.9.9 Step 4: Converting Image to Grayscale

In [323]:
#Converting the image to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray.shape
rgb_for_metplot(gray, cv2.COLOR_GRAY2RGB, "Grayscale Image")
No description has been provided for this image
In [324]:
plt.imshow(gray, cmap='gray')
Out[324]:
<matplotlib.image.AxesImage at 0x1b3ffe5f350>
No description has been provided for this image

5.9.10 Step 5: Loading Haar Cascades for Face and Eye Detection

In [325]:
import cv2

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
In [326]:
faces
Out[326]:
array([[43, 56, 97, 97]], dtype=int32)
In [327]:
print(f"Is classifier empty? {face_cascade.empty()}") 

if not face_cascade.empty():
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    print(f"Faces found: {len(faces)}")
else:
    print("XML file load nahi hui, path sahi karein!")
Is classifier empty? False
Faces found: 1
In [328]:
(x,y,w,h) = faces[0]
x,y,w,h
Out[328]:
(np.int32(43), np.int32(56), np.int32(97), np.int32(97))

5.9.11 Step 6: Detecting Faces and Drawing Rectangles Around Them

In [329]:
face_img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
plt.imshow(face_img)
Out[329]:
<matplotlib.image.AxesImage at 0x1b380245e80>
No description has been provided for this image

5.9.12 Step 7: Detecting Faces , eyes and Drawing Rectangles Around Them

In [330]:
cv2.destroyAllWindows()
for (x,y,w,h) in faces:
    face_img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = face_img[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
        

plt.figure()
plt.imshow(face_img, cmap='gray')
plt.show()
No description has been provided for this image
In [331]:
#Plotting the cropped Image
%matplotlib inline
plt.imshow(roi_color, cmap='gray')
plt.show()
No description has been provided for this image
In [332]:
cropped_img = np.array(roi_color)
cropped_img.shape
Out[332]:
(97, 97, 3)

5.9.13 Step 8: Performing a 2D wavelet transform on an image

In [333]:
import numpy as np
import pywt
import cv2    

def w2d(img, mode='haar', level=1):
    imArray = img
    #Datatype conversions
    #convert to grayscale
    imArray = cv2.cvtColor( imArray,cv2.COLOR_RGB2GRAY )
    #convert to float
    imArray =  np.float32(imArray)   
    imArray /= 255;
    # compute coefficients 
    coeffs=pywt.wavedec2(imArray, mode, level=level)

    #Process Coefficients
    coeffs_H=list(coeffs)  
    coeffs_H[0] *= 0;  

    # reconstruction
    imArray_H=pywt.waverec2(coeffs_H, mode);
    imArray_H *= 255;
    imArray_H =  np.uint8(imArray_H)

    return imArray_H

5.9.14 Step 9: Function to Get Cropped Image with Two Eyes

In [334]:
im_har = w2d(cropped_img,'db1',5)
plt.imshow(im_har, cmap='gray')
Out[334]:
<matplotlib.image.AxesImage at 0x1b39848ab70>
No description has been provided for this image

5.9.15 Step 10: Preprocessing: Load image, detect face. If eyes >=2, then save and crop the face region

In [335]:
def get_cropped_image_if_2_eyes(image_path):
    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        if len(eyes) >= 2:
            return roi_color
          
original_image = cv2.imread('content/face.jpg')
plt.imshow(original_image)
Out[335]:
<matplotlib.image.AxesImage at 0x1b3802b0200>
No description has been provided for this image
In [336]:
cropped_image = get_cropped_image_if_2_eyes('content/face.jpg')
plt.imshow(cropped_image)
Out[336]:
<matplotlib.image.AxesImage at 0x1b385a8eb10>
No description has been provided for this image

5.9.16 Step 9: Preparing and Organizing the Dataset

In [337]:
path_to_data = "./images/"
path_to_cr_data = "./images/cropped/"

import os
img_dirs = []
for entry in os.scandir(path_to_data):
    if entry.is_dir():
        img_dirs.append(entry.path)
img_dirs
Out[337]:
['./images/bhuvneshwar_kumar',
 './images/dinesh_karthik',
 './images/hardik_pandya',
 './images/jasprit_bumrah',
 './images/mohammed_shami',
 './images/ms_dhoni']
In [338]:
import shutil
if os.path.exists(path_to_cr_data):
     shutil.rmtree(path_to_cr_data)
os.mkdir(path_to_cr_data)
In [339]:
cropped_image_dirs = []
celebrity_file_names_dict = {}

for img_dir in img_dirs:
    count = 1
    celebrity_name = img_dir.split('/')[-1]
    print(celebrity_name)
    
    celebrity_file_names_dict[celebrity_name] = []
    
    for entry in os.scandir(img_dir):
        roi_color = get_cropped_image_if_2_eyes(entry.path)
        if roi_color is not None:
            cropped_folder = path_to_cr_data + celebrity_name
            if not os.path.exists(cropped_folder):
                os.makedirs(cropped_folder)
                cropped_image_dirs.append(cropped_folder)
                print("Generating cropped images in folder: ",cropped_folder)
                
            cropped_file_name = celebrity_name + str(count) + ".png"
            cropped_file_path = cropped_folder + "/" + cropped_file_name 
            
            cv2.imwrite(cropped_file_path, roi_color)
            celebrity_file_names_dict[celebrity_name].append(cropped_file_path)
            count += 1
bhuvneshwar_kumar
Generating cropped images in folder:  ./images/cropped/bhuvneshwar_kumar
dinesh_karthik
Generating cropped images in folder:  ./images/cropped/dinesh_karthik
hardik_pandya
Generating cropped images in folder:  ./images/cropped/hardik_pandya
jasprit_bumrah
Generating cropped images in folder:  ./images/cropped/jasprit_bumrah
mohammed_shami
Generating cropped images in folder:  ./images/cropped/mohammed_shami
ms_dhoni
Generating cropped images in folder:  ./images/cropped/ms_dhoni

5.9.17 Step 10: Creating a Dictionary for Training Files

In [340]:
celebrity_file_names_dict = {}
for img_dir in cropped_image_dirs:
    celebrity_name = img_dir.split('/')[-1]
    file_list = []
    for entry in os.scandir(img_dir):
        file_list.append(entry.path)
    celebrity_file_names_dict[celebrity_name] = file_list
celebrity_file_names_dict
Out[340]:
{'bhuvneshwar_kumar': ['./images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar1.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar10.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar11.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar12.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar2.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar3.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar4.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar5.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar6.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar7.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar8.png',
  './images/cropped/bhuvneshwar_kumar\\bhuvneshwar_kumar9.png'],
 'dinesh_karthik': ['./images/cropped/dinesh_karthik\\dinesh_karthik1.png',
  './images/cropped/dinesh_karthik\\dinesh_karthik2.png',
  './images/cropped/dinesh_karthik\\dinesh_karthik3.png',
  './images/cropped/dinesh_karthik\\dinesh_karthik4.png',
  './images/cropped/dinesh_karthik\\dinesh_karthik5.png',
  './images/cropped/dinesh_karthik\\dinesh_karthik6.png',
  './images/cropped/dinesh_karthik\\dinesh_karthik7.png',
  './images/cropped/dinesh_karthik\\dinesh_karthik8.png',
  './images/cropped/dinesh_karthik\\dinesh_karthik9.png'],
 'hardik_pandya': ['./images/cropped/hardik_pandya\\hardik_pandya1.png',
  './images/cropped/hardik_pandya\\hardik_pandya10.png',
  './images/cropped/hardik_pandya\\hardik_pandya2.png',
  './images/cropped/hardik_pandya\\hardik_pandya3.png',
  './images/cropped/hardik_pandya\\hardik_pandya4.png',
  './images/cropped/hardik_pandya\\hardik_pandya5.png',
  './images/cropped/hardik_pandya\\hardik_pandya6.png',
  './images/cropped/hardik_pandya\\hardik_pandya7.png',
  './images/cropped/hardik_pandya\\hardik_pandya8.png',
  './images/cropped/hardik_pandya\\hardik_pandya9.png'],
 'jasprit_bumrah': ['./images/cropped/jasprit_bumrah\\jasprit_bumrah1.png',
  './images/cropped/jasprit_bumrah\\jasprit_bumrah2.png',
  './images/cropped/jasprit_bumrah\\jasprit_bumrah3.png',
  './images/cropped/jasprit_bumrah\\jasprit_bumrah4.png'],
 'mohammed_shami': ['./images/cropped/mohammed_shami\\mohammed_shami1.png',
  './images/cropped/mohammed_shami\\mohammed_shami2.png',
  './images/cropped/mohammed_shami\\mohammed_shami3.png',
  './images/cropped/mohammed_shami\\mohammed_shami4.png',
  './images/cropped/mohammed_shami\\mohammed_shami5.png',
  './images/cropped/mohammed_shami\\mohammed_shami6.png',
  './images/cropped/mohammed_shami\\mohammed_shami7.png',
  './images/cropped/mohammed_shami\\mohammed_shami8.png'],
 'ms_dhoni': ['./images/cropped/ms_dhoni\\ms_dhoni1.png',
  './images/cropped/ms_dhoni\\ms_dhoni10.png',
  './images/cropped/ms_dhoni\\ms_dhoni11.png',
  './images/cropped/ms_dhoni\\ms_dhoni12.png',
  './images/cropped/ms_dhoni\\ms_dhoni13.png',
  './images/cropped/ms_dhoni\\ms_dhoni14.png',
  './images/cropped/ms_dhoni\\ms_dhoni2.png',
  './images/cropped/ms_dhoni\\ms_dhoni3.png',
  './images/cropped/ms_dhoni\\ms_dhoni4.png',
  './images/cropped/ms_dhoni\\ms_dhoni5.png',
  './images/cropped/ms_dhoni\\ms_dhoni6.png',
  './images/cropped/ms_dhoni\\ms_dhoni7.png',
  './images/cropped/ms_dhoni\\ms_dhoni8.png',
  './images/cropped/ms_dhoni\\ms_dhoni9.png']}

5.9.18 Step 11: Creating a Class Dictionary

In [341]:
class_dict = {}
count = 0
for celebrity_name in celebrity_file_names_dict.keys():
    class_dict[celebrity_name] = count
    count = count + 1
class_dict
Out[341]:
{'bhuvneshwar_kumar': 0,
 'dinesh_karthik': 1,
 'hardik_pandya': 2,
 'jasprit_bumrah': 3,
 'mohammed_shami': 4,
 'ms_dhoni': 5}

5.9.19 Step 12: Preparing the Feature Vectors

In [342]:
X, y = [], []
for celebrity_name, training_files in celebrity_file_names_dict.items():
    for training_image in training_files:
        img = cv2.imread(training_image)
        if img is None:
            continue
        
        scalled_raw_img = cv2.resize(img, (32, 32))
        
        img_har = w2d(img, 'db1', 5)
        scalled_img_har = cv2.resize(img_har, (32, 32))
        
        combined_img = np.vstack((
            scalled_raw_img.reshape(32*32*3, 1), 
            scalled_img_har.reshape(32*32, 1)
        )).flatten() 
        
        X.append(combined_img)
        y.append(class_dict[celebrity_name])


X = np.array(X).astype(float)
y = np.array(y)

5.9.20 Step 13: Training the SVM Classifier

In [343]:
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

pipe = Pipeline([('scaler', StandardScaler()), ('svc', SVC(kernel = 'rbf', C = 10))])
pipe.fit(X_train, y_train)
pipe.score(X_test, y_test)
Out[343]:
0.4
In [344]:
print(classification_report(y_test, pipe.predict(X_test)))
              precision    recall  f1-score   support

           0       0.60      0.75      0.67         4
           1       0.00      0.00      0.00         0
           2       1.00      0.33      0.50         3
           3       0.00      0.00      0.00         3
           4       0.00      0.00      0.00         2
           5       0.33      0.67      0.44         3

    accuracy                           0.40        15
   macro avg       0.32      0.29      0.27        15
weighted avg       0.43      0.40      0.37        15

c:\Users\HP\OneDrive\Desktop\OPEN CV\whyi\Lib\site-packages\sklearn\metrics\_classification.py:1833: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, f"{metric.capitalize()} is", result.shape[0])
c:\Users\HP\OneDrive\Desktop\OPEN CV\whyi\Lib\site-packages\sklearn\metrics\_classification.py:1833: UndefinedMetricWarning: Recall is ill-defined and being set to 0.0 in labels with no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, f"{metric.capitalize()} is", result.shape[0])
c:\Users\HP\OneDrive\Desktop\OPEN CV\whyi\Lib\site-packages\sklearn\metrics\_classification.py:1833: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, f"{metric.capitalize()} is", result.shape[0])
c:\Users\HP\OneDrive\Desktop\OPEN CV\whyi\Lib\site-packages\sklearn\metrics\_classification.py:1833: UndefinedMetricWarning: Recall is ill-defined and being set to 0.0 in labels with no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, f"{metric.capitalize()} is", result.shape[0])
c:\Users\HP\OneDrive\Desktop\OPEN CV\whyi\Lib\site-packages\sklearn\metrics\_classification.py:1833: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, f"{metric.capitalize()} is", result.shape[0])
c:\Users\HP\OneDrive\Desktop\OPEN CV\whyi\Lib\site-packages\sklearn\metrics\_classification.py:1833: UndefinedMetricWarning: Recall is ill-defined and being set to 0.0 in labels with no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, f"{metric.capitalize()} is", result.shape[0])

6 Feature Detection & Analysis

6.1 Find Co-ordinates of Contours

6.1.1 Python Implementation

In [345]:
import numpy as np
import cv2

font = cv2.FONT_HERSHEY_COMPLEX

# Load image
img2 = cv2.imread('content/test.jpg', cv2.IMREAD_COLOR)
img = cv2.imread('content/test.jpg', cv2.IMREAD_GRAYSCALE)

# Binarize image
_, threshold = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY)

# Find contours
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    # Approximate and draw contour
    approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True)
    cv2.drawContours(img2, [approx], 0, (0, 0, 255), 5)

    # Flatten points
    n = approx.ravel()
    i = 0
    for j in n:
        if i % 2 == 0:  # x, y coords
            x, y = n[i], n[i + 1]
            coord = f"{x} {y}"
            if i == 0:  # first point
                cv2.putText(img2, "Arrow tip", (x, y), font, 0.5, (255, 0, 0))
            else:
                cv2.putText(img2, coord, (x, y), font, 0.5, (0, 255, 0))
        i += 1

# Show result
cv2.imshow('Contours with Coordinates', img2)

# Exit on 'q'
if cv2.waitKey(0) & 0xFF == ord('q'):
    cv2.destroyAllWindows()

plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
plt.title("Contours with Coordinates")
plt.axis('off')
plt.show()
No description has been provided for this image

6.2 Analyze an image using Histogram

6.2.1 Importing image data

In [346]:
import matplotlib.pyplot as plt
import cv2

img = plt.imread(tomatoes)  
plt.imshow(img)
plt.title("Original Image")
plt.show()
No description has been provided for this image

6.2.2 Creating Histogram using Numpy & Matplotlib

In [347]:
import cv2, matplotlib.pyplot as plt
img = cv2.imread(tomatoes)

# Grayscale + Histogram
g = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.subplot(121), plt.imshow(g, cmap='gray'), plt.axis("off"), plt.title("Grayscale")
plt.subplot(122), plt.hist(g.ravel(),256,[0,256],color='k'), plt.title("Gray Histogram")
plt.show()

# RGB Histograms
for i,c in enumerate(('r','g','b')):
    plt.plot(cv2.calcHist([img],[i],None,[256],[0,256]), color=c)
plt.title("RGB Histograms"), plt.xlabel("Intensity"), plt.ylabel("Frequency")
plt.show()
C:\Users\HP\AppData\Local\Temp\ipykernel_4036\1510350441.py:7: MatplotlibDeprecationWarning: Passing the range parameter of hist() positionally is deprecated since Matplotlib 3.10; the parameter will become keyword-only in 3.12.
  plt.subplot(122), plt.hist(g.ravel(),256,[0,256],color='k'), plt.title("Gray Histogram")
No description has been provided for this image
No description has been provided for this image

6.2.3 Histogram Calculation using OpenCV

In [348]:
img = cv2.imread(tomatoes, 0)
histg = cv2.calcHist([img], [0], None, [256], [0, 256])

# Plot histogram
plt.plot(histg)
plt.title("Histogram using OpenCV calcHist()")
plt.xlabel("Pixel Intensity")
plt.ylabel("Frequency")
plt.show()
No description has been provided for this image

6.2.4 Histogram for Color Images

In [349]:
img = cv2.imread(tomatoes)

# colors for channels
colors = ('b', 'g', 'r')

for i, col in enumerate(colors):
    hist = cv2.calcHist([img], [i], None, [256], [0, 256])
    plt.plot(hist, color=col)
    plt.xlim([0, 256])

plt.title("RGB Color Histogram")
plt.xlabel("Pixel Intensity")
plt.ylabel("Frequency")
plt.show()
No description has been provided for this image

6.2.5 Using OpenCV (cvcalcHist())

In [350]:
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(tomatoes,0)

histr = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(histr)
plt.show()
No description has been provided for this image

6.2.6 Using plt.hist()

In [ ]:
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(tomatoes,0)

# alternative way to find histogram of an image
plt.hist(img.ravel(),256,[0,256])
plt.show()
C:\Users\HP\AppData\Local\Temp\ipykernel_4036\1419790081.py:6: MatplotlibDeprecationWarning: Passing the range parameter of hist() positionally is deprecated since Matplotlib 3.10; the parameter will become keyword-only in 3.12.
  plt.hist(img.ravel(),256,[0,256])
No description has been provided for this image
The Kernel crashed while executing code in the current cell or a previous cell. 

Please review the code in the cell(s) to identify a possible cause of the failure. 

Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. 

View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.